package com.lh_travel.lohas.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.graphics.Paint;
import android.support.v7.widget.RecyclerView;
import android.util.SparseArray;
import android.view.View;
import android.widget.CheckedTextView;
import android.widget.ImageView;
import android.widget.TextView;

import com.squareup.picasso.Picasso;

/**
 * Created by aaron on 2018/6/6.
 */

public class BaseRecyclerHolder extends RecyclerView.ViewHolder {

    private SparseArray<View> views;
    private Context context;

    public BaseRecyclerHolder(Context context, View itemView) {
        super(itemView);
        this.context = context;
        //指定一个初始为8
        views = new SparseArray<>(8);
    }

    /**
     * 取得一个RecyclerHolder对象
     *
     * @param context  上下文
     * @param itemView 子项
     * @return 返回一个RecyclerHolder对象
     */
    public static BaseRecyclerHolder getRecyclerHolder(Context context, View itemView) {
        return new BaseRecyclerHolder(context, itemView);
    }

    public SparseArray<View> getViews() {
        return this.views;
    }

    /**
     * 通过view的id获取对应的控件，如果没有则加入views中
     *
     * @param viewId 控件的id
     * @return 返回一个控件
     */
    @SuppressWarnings("unchecked")
    public <T extends View> T getView(int viewId) {
        View view = views.get(viewId);
        if (view == null) {
            view = itemView.findViewById(viewId);
            views.put(viewId, view);
        }
        return (T) view;
    }

    /**
     * 设置字符串
     */
    public BaseRecyclerHolder setText(int viewId, String text) {
        TextView tv = getView(viewId);
        tv.setText(text);
        return this;
    }

    /**
     * 设置字符串
     */
    public BaseRecyclerHolder setTextcheck(int viewId, String text) {
        CheckedTextView tv = getView(viewId);
        tv.setText(text);
        return this;
    }

    /**
     * 设置字符串和颜色
     */
    public BaseRecyclerHolder setTextandColor(int viewId, String text, String color) {
        TextView tv = getView(viewId);
        tv.setTextColor(Color.parseColor(color));
        tv.setText(text);
        return this;
    }

    /**
     * 设置图片
     */
    public BaseRecyclerHolder setImageResource(int viewId, int drawableId) {
        ImageView iv = getView(viewId);
        iv.setImageResource(drawableId);
        return this;
    }

    /**
     * 设置图片
     */
    public BaseRecyclerHolder setImageBitmap(int viewId, Bitmap bitmap) {
        ImageView iv = getView(viewId);

        iv.setImageBitmap(bitmap);
        return this;
    }

    /**
     * 设置图片
     */
    public BaseRecyclerHolder setImageByUrl(int viewId, String url, int placeHolder) {
        ImageView iv = getView(viewId);
        if (url != null && url.length() > 0) {
            Picasso.with(context).load(url).
                    placeholder(placeHolder).
            //error(R.drawable.default_bg100x100).
            fit().centerCrop().
                    into(iv);
        }
        return this;
    }

    public BaseRecyclerHolder setImageByUrl2(int viewId, String url, int placeHolder) {
        ImageView iv = getView(viewId);
        if (url != null && url.length() > 0) {
            Picasso.with(context).load(url).
                    placeholder(placeHolder).
                    //error(R.drawable.default_bg100x100).
                            fit().centerInside().
                    into(iv);
        }
        return this;
    }

    /**
     * 设置控件是否显示
     *
     * @param viewId
     * @param vis
     */
    public void setViewsVisable(int viewId, boolean vis) {
        View iv = getView(viewId);
        iv.setVisibility(vis ? View.VISIBLE : View.GONE);
    }

    /**
     * 设置控件的点击事件
     *
     * @param viewId
     * @param Listener
     */
    public void setViewClickLisenter(int viewId, View.OnClickListener Listener) {
        View iv = getView(viewId);
        iv.setOnClickListener(Listener);
    }

    public void setTextBackground(int viewId, int ResID) {
        TextView iv = getView(viewId);

        iv.setBackgroundResource(ResID);
    }

    public void setTextSelect(int viewId, boolean select) {
        TextView iv = getView(viewId);

        iv.setSelected(select);

    }

    public void setViewBG(int viewId, int res) {
        View iv = getView(viewId);

        iv.setBackgroundResource(res);

    }

    public void setTextFlag(int viweId, int flag) {
        TextView iv = getView(viweId);

        switch (flag) {
            case 1:
                //添加删除线
                iv.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG);
                break;

            case 2:
                //在代码中设置加粗
                iv.getPaint().setFlags(Paint.FAKE_BOLD_TEXT_FLAG);
                break;

            case 3:
                //添加下划线
                iv.getPaint().setFlags(Paint.UNDERLINE_TEXT_FLAG);
                break;
        }

    }

}
